package com.igeek.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @author jerry
 * @Company 极客营
 *
 */
/* <!--定义通知类-->
    <bean id="sqlExTimeAspact" class="com.igeek.aop.SqlExTimeAspact"></bean>
    <!--配置AOP-->
    <aop:config>
        <!--配置切入点，需要拦截哪个类的方法-->
        <aop:pointcut id="daoPointCut" expression="bean(*Dao)"/>
        <!--配置切面-->
        <aop:aspect ref="sqlExTimeAspact">
            <!--配置通知-->
            <aop:around method="aroundDao" pointcut-ref="daoPointCut"></aop:around>
        </aop:aspect>
    </aop:config>*/
@Component
@Aspect
public class SqlExTimeAspact {
    Logger logger = Logger.getLogger(SqlExTimeAspact.class);
    //编写环绕通知,返回sql的执行时间
    @Around("bean(*Dao)")
    public Object aroundDao(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        long start = System.currentTimeMillis();
        //执行方法
        Object o = proceedingJoinPoint.proceed();
        long end = System.currentTimeMillis();
        logger.info(proceedingJoinPoint.getTarget().getClass().getName()
        +"--"+proceedingJoinPoint.getSignature().getName()+":执行了"
        +(end - start)+"毫秒");
        return o;
    }
}
